<?php
$meta_box_quote = array(
	'id' => 'redox-meta-box-quote',
	'title' =>  __('Quotation', 'redox_localize'),
	'page' => 'post',
	'context' => 'normal',
	'priority' => 'high',
	'fields' => array(
		array( "name" => __('The Quote','redox_localize'),
				"desc" => __('Write your quote in this field.','redox_localize'),
				"id" => "redox_quote",
				"type" => "textarea"
			),
	),
	
	
);


$meta_box_link = array(
	'id' => 'redox-meta-box-link',
	'title' =>  __('Link URL', 'redox_localize'),
	'page' => 'post',
	'context' => 'normal',
	'priority' => 'high',
	'fields' => array(
		array( "name" => __('The Link','redox_localize'),
				"desc" => __('Enter the URL you wish to link to.','redox_localize'),
				"id" => "redox_link",
				"type" => "text"
			),
	),
	
);

$meta_box_image= array(
	'id' => 'redox-meta-box-image',
	'title' =>  __('Image URL', 'redox_localize'),
	'page' => 'post',
	'context' => 'normal',
	'priority' => 'high',
	'fields' => array(
		array( "name" => __('Image Link URL','redox_localize'),
				"desc" => __('Enter the URL the image will link to. Defaults to the permalink page.','redox_localize'),
				"id" => "redox_image",
				"type" => "text"
			),
	),
	
);




$meta_box_video = array(
	'id' => 'redox-meta-box-video',
	'title' =>  __('Video Embed Code', 'redox_localize'),
	'page' => 'post',
	'context' => 'normal',
	'priority' => 'high',
	'fields' => array(
		array( "name" => __('Video Embed Code','redox_localize'),
				"desc" => __('Place the video embed code here.<br><br> For Youtube and Vimeo embeds simply place the page url.','redox_localize'),
				"id" => "redox_video",
				"type" => "textarea"
			),
	),
	
);





add_action('admin_menu', 'redox_add_box');


/* ---------------------------------------------------------------------- 

	Adds custom meta boxes to edit page.
		
----------------------------------------------------------------------- */ 


 
function redox_add_box() {

	global $meta_box_quote, $meta_box_link,  $meta_box_video, $meta_box_image;
 
	add_meta_box($meta_box_quote['id'], $meta_box_quote['title'], 'redox_show_box_quote', $meta_box_quote['page'], $meta_box_quote['context'], $meta_box_quote['priority']);
	add_meta_box($meta_box_video['id'], $meta_box_video['title'], 'redox_show_box_video', $meta_box_video['page'], $meta_box_video['context'], $meta_box_video['priority']);
	add_meta_box($meta_box_link['id'], $meta_box_link['title'], 'redox_show_box_link', $meta_box_link['page'], $meta_box_link['context'], $meta_box_link['priority']);
	add_meta_box($meta_box_image['id'], $meta_box_image['title'], 'redox_show_box_image', $meta_box_image['page'], $meta_box_image['context'], $meta_box_image['priority']);

}


/* ---------------------------------------------------------------------- 

	Show custom meta boxes on edit page.
		
----------------------------------------------------------------------- */ 


function redox_show_box_quote() {
global $meta_box_quote, $post;

echo '<input type="hidden" name="redox_meta_box_nonce" value="'. wp_create_nonce( basename( __file__ ) ). '" />';
echo '<table class="form-table">';

foreach ( $meta_box_quote['fields'] as $field ) {
    $meta = get_post_meta( $post->ID, $field['id'], true );
    switch ( $field['type'] ) {
        case 'textarea':
            echo '<tr>', '<th style="width:25%"><label for="', $field['id'], '"><strong>', $field['name'], '</strong><span style="line-height:18px; display:block; color:#999; margin:5px 0 0 0;">' . $field['desc'] . '</span></label></th><td>';
            echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '"  rows="8" cols="5" style="width:100%; margin-right: 20px; float:left;">'. $meta.'</textarea>';
            break;
    }

}

echo '</table>';
}

function redox_show_box_video() {
global $meta_box_video, $post;

echo '<input type="hidden" name="redox_meta_box_nonce" value="'. wp_create_nonce( basename( __file__ ) ). '" />';
echo '<table class="form-table">';

foreach ( $meta_box_video['fields'] as $field ) {
    $meta = get_post_meta( $post->ID, $field['id'], true );
    switch ( $field['type'] ) {
        case 'textarea':
            echo '<tr>', '<th style="width:25%"><label for="', $field['id'], '"><strong>', $field['name'], '</strong><span style="line-height:18px; display:block; color:#999; margin:5px 0 0 0;">' . $field['desc'] . '</span></label></th><td>';
            echo '<textarea name="'. $field['id']. '" id="'. $field['id']. '"  rows="8" cols="5" style="width:100%; margin-right: 20px; float:left;">'. $meta.'</textarea>';
            break;
    }

}

echo '</table>';
}

function redox_show_box_link() {
global $meta_box_link, $post;

echo '<input type="hidden" name="redox_meta_box_nonce" value="'. wp_create_nonce( basename( __file__ ) ). '" />';
echo '<table class="form-table">';

foreach ( $meta_box_link['fields'] as $field ) {
    $meta = get_post_meta( $post->ID, $field['id'], true );
    switch ( $field['type'] ) {
        case 'text':
            echo '<tr>', '<th style="width:25%"><label for="', $field['id'], '"><strong>', $field['name'], '</strong><span style="line-height:18px; display:block; color:#999; margin:5px 0 0 0;">' . $field['desc'] . '</span></label></th><td>';
            echo '<input type="text" name="'. $field['id']. '" id="'. $field['id']. '"  size="30" style="width:75%; margin-right: 20px; float:left;" value="' . $meta . '"></input>';
            break;
    }

}

echo '</table>';
}


function redox_show_box_image() {
global $meta_box_image, $post;

echo '<input type="hidden" name="redox_meta_box_nonce" value="'. wp_create_nonce( basename( __file__ ) ). '" />';
echo '<table class="form-table">';

foreach ( $meta_box_image['fields'] as $field ) {
    $meta = get_post_meta( $post->ID, $field['id'], true );
    switch ( $field['type'] ) {
        case 'text':
            echo '<tr>', '<th style="width:25%"><label for="', $field['id'], '"><strong>', $field['name'], '</strong><span style="line-height:18px; display:block; color:#999; margin:5px 0 0 0;">' . $field['desc'] . '</span></label></th><td>';
            echo '<input type="text" name="'. $field['id']. '" id="'. $field['id']. '"  size="30" style="width:75%; margin-right: 20px; float:left;" value="' . $meta . '"></input>';
            break;
    }

}

echo '</table>';
}


/* ---------------------------------------------------------------------- 

	Save custom meta box data.
		
----------------------------------------------------------------------- */ 
 
function redox_save_data($post_id) {
	global $meta_box_quote, $meta_box_video, $meta_box_link, $meta_box_image;
 
	if ( !isset($_POST['redox_meta_box_nonce']) || !wp_verify_nonce( $_POST['redox_meta_box_nonce'], basename(__FILE__))) {
		return $post_id;
	}
 
 	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
		return $post_id;
	}
 
 	if ('page' == $_POST['post_type']) {
		if (!current_user_can('edit_page', $post_id)) {
			return $post_id;
		}
	} elseif (!current_user_can('edit_post', $post_id)) {
		return $post_id;
	}
 
	foreach ($meta_box_quote['fields'] as $field) {
		  $old = get_post_meta( $post_id, $field['id'], true );
        if ( isset($_POST[$field['id']])) { $new = $_POST[$field['id']]; }

        if ( isset( $new ) && ! empty( $new ) && ( $new != $old ) ) {
            update_post_meta( $post_id, $field['id'], $new );
        } elseif ( '' == $new && $old ) {
            delete_post_meta( $post_id, $field['id'], $old );
        }
	}


	foreach ($meta_box_image['fields'] as $field) {
		  $old = get_post_meta( $post_id, $field['id'], true );
        if ( isset($_POST[$field['id']])) { $new = $_POST[$field['id']]; }

        if ( isset( $new ) && ! empty( $new ) && ( $new != $old ) ) {
            update_post_meta( $post_id, $field['id'], $new );
        } elseif ( '' == $new && $old ) {
            delete_post_meta( $post_id, $field['id'], $old );
        }
	}


	foreach ($meta_box_link['fields'] as $field) {
		  $old = get_post_meta( $post_id, $field['id'], true );
        if ( isset($_POST[$field['id']])) { $new = $_POST[$field['id']]; }

        if ( isset( $new ) && ! empty( $new ) && ( $new != $old ) ) {
            update_post_meta( $post_id, $field['id'], $new );
        } elseif ( '' == $new && $old ) {
            delete_post_meta( $post_id, $field['id'], $old );
        }
	}


	foreach ($meta_box_video['fields'] as $field) {
		  $old = get_post_meta( $post_id, $field['id'], true );
        if ( isset($_POST[$field['id']])) { $new = $_POST[$field['id']]; }

        if ( isset( $new ) && ! empty( $new ) && ( $new != $old ) ) {
            update_post_meta( $post_id, $field['id'], $new );
        } elseif ( '' == $new && $old ) {
            delete_post_meta( $post_id, $field['id'], $old );
        }
	}

}

add_action('save_post', 'redox_save_data');

function redox_admin_js($hook) {
	if ($hook == 'post.php' || $hook == 'post-new.php') {
		wp_register_script('redox-admin', get_template_directory_uri() . '/js/meta_boxes.js', 'jquery');
		wp_enqueue_script('redox-admin');
	}
}

add_action('admin_enqueue_scripts','redox_admin_js');
?>